<?php
/**
 * Created by PhpStorm.
 * User: zhyg
 * Date: 2015/8/21
 * Time: 13:39
   $_FILES = array(1) {
        ["avatar"]=>
            array(5) {
                ["name"]=>
                string(26) "shuffling04.png"
                ["type"]=>
                string(9) "image/png"
                ["tmp_name"]=>
                string(46) "C:\Users\j28981\AppData\Local\Temp\php81A7.tmp"
                ["error"]=>
                int(0)
                ["size"]=>
                int(146040)
              }
        }
 * 上传文件 图片,单图或多图
 */
class Upload {
    private $file_upload_root;   //文件上传绝对根路径，其下可以自己新建路径
    private $file_upload_dir='';   //文件上传之新建绝对路径 'd:xxx/upload/stu/201508/'
    private $file_allow_types;        //允许MIME类型,用','隔开
    private $file_maxsize;
    private $file_array;        //从$_FILES 中取出的一维数组
    private $file_name_new='';     //随机文件名
    private $file_upload_name='';  //保存在数据库中的图片路径 'upload/...'
    public $error='';
    const Y=1;
    const M=2;
    const D=3;
    //初始化属性：从配置文件中取出
    /**
     * @param String $input_name：在文件上传表单的name属性值
     * @param string $folder
     * @param int $date_type
     */
    public function __construct($input_name,$folder='images',$date_type=2){
        $this->file_upload_root = $GLOBALS['config']['upload']['file_upload_root'];
        $this->file_allow_types = $GLOBALS['config']['upload']['file_allow_types'];
        $this->file_maxsize = $GLOBALS['config']['upload']['file_maxsize'];
        $this->file_array = isset($_FILES[$input_name])?$_FILES[$input_name]:'';
        $this->makeDir($folder,$date_type);
    }

    /**
     *上传一张图片到指定的目录
     * @return mixed String $this->file_upload_name  or false
     */
    public function uploadOne(){
        if($this->file_array==''){
            $this->error = "没有文件上传";
            return false;
        }
        switch($this->file_array['error']){
            case 1:
                $this->error = '文件超过服务器允许的大小！最大允许：'.ini_get('upload_max_filesize');     //允许为2M
                return false;
            case 2:
                $this->error = '文件超过浏览器允许的大小！';
                return false;
            case 3:
                $this->error ="文件上传中断！";
                return false;
            case 4:
                self:$error = "没有选中要上传的文件";
                return false;
            case 6:
            case 7:
                $this->error = '服务器错误！';
        }
        if(strpos($this->file_allow_types,$this->file_array['type'])===false){
            $this->error = "文件类型不正确，可以上传类型$this->file_allow_types";
            return false;
        }
        if($this->file_array['size']>$this->file_maxsize){
            $this->error = "文件大小超过允许，最大不能超过1M";
            return false;
        }
        $this->file_name_new = $this->getNewName($this->file_array['name']);

        $file_name_root =$this->file_upload_dir.DS.$this->file_name_new;
        $this->file_upload_name = strchr($file_name_root,'Upload');
        if(move_uploaded_file($this->file_array['tmp_name'],$file_name_root)){
            return $this->file_upload_name;    //返回值，用于存放数据库
        }else{
            $this->error = "系统错误，最后没有上传";
            return false;
        }
    }
    private function getNewName($file_name_old){
        $file_name_new = date('YmdHis');
        $str_all = implode('',array_merge(range(0,9),range('a','z'),range('A','Z')));
        for($i=0,$len=strlen($str_all);$i<5;$i++){
            $file_name_new .= $str_all[mt_rand(0,$len-1)];
        }
        $ext = strrchr($file_name_old,'.');
        $file_name_new.= $ext;
        return $file_name_new;
    }

    /**
     * 自定义upload下的路径名称，并在其下按年月日分文件夹
     * @param String $folder: 自定义的文件夹名
     * @param int $date_type 1表示按年，2按年月，3按年月日
     */
    private function makeDir($folder,$date_type){

        if($date_type==1){
            $file_dir_str = $this->file_upload_root.DS.$folder.DS.date('Y');
            if(!is_dir($file_dir_str)) {
                if (mkdir($file_dir_str, '0777', true)) {
                    $this->file_upload_dir = $file_dir_str;
                } else {
                    echo $this->error = "新建路径失败";
                }
            }else{
                $this->file_upload_dir = $file_dir_str;
            }
        }elseif($date_type==2){

            $file_dir_str = $this->file_upload_root.DS.$folder.DS.date('Y/m');
            if(!is_dir($file_dir_str)){
                if(mkdir ($file_dir_str,'0777',true)){
                    $this->file_upload_dir = $file_dir_str;
                }else {
                    echo $this->error = "新建路径失败";
                }
            }else{
                $this->file_upload_dir = $file_dir_str;
            }

        }elseif($date_type==3){
            $file_dir_str = $this->file_upload_root.DS.$folder.DS.date('Y/m/d');
            if(!is_dir($file_dir_str)){
                if(mkdir ($file_dir_str,'0777',true)){
                    $this->file_upload_dir=$file_dir_str;
                }else {
                    echo $this->error = "新建路径失败";
                }
            }else{
                $this->file_upload_dir = $file_dir_str;
            }

        }
    }
}